{include file="../app/view/common/header.html" subtitle="登录" page="user"}


    <style>
        /* body
        {
            background:url(/static/user/auth/bg.jpg);
            -webkit-background-size: cover;
            -o-background-size: cover;                
            background-size: cover;
            overflow-y: hidden;
        } */
    </style>
    <link rel="stylesheet" type="text/css" href="/static/css.css">
    <link rel="stylesheet" type="text/css" href="/static/user/auth/login.css">
</head>
<body>
    <div class="login">
        <h1>登录</h1>
        <h4>不管！信管2301就是最强的！</h4>
        <br><br>
        <div class="login-div">
            <label>用户名</label>
            <input class="pp-input" placeholder="用户名" id="username">
        </div>
        <div class="login-div">
            <label>密码</label>
            <input class="pp-input" type="password" placeholder="密码" id="password">
        </div>
        <div class="login-div" id="repassword_div" style="display: none;">
            <label>重复密码</label>
            <input class="pp-input" type="password" placeholder="重复密码" id="repassword">
        </div>
        <button class="mdui-btn mdui-color-theme-accent mdui-ripple" onclick="work();" id="button">点我登录!</button>
    </div>
    <script type="text/javascript">
        // 判断 登录 or 注册
        loginpage = true
        //if(window.location.href.indexOf('reg') != -1) loginpage = false

        // 如果是注册，则要重复密码的框
        if (!loginpage) {
            document.getElementById('repassword_div').style.display = ''
            document.getElementById('button').innerHTML = '点我注册！'
        }

        function work() {
            if (loginpage) login()
            else register()
        }

        function login() {
            var username = document.getElementById('username').value
            var password = document.getElementById('password').value
            if(username === '' || password === '') {
                error_bar('有东东没输入哦')
                return;
            }
            if(username.length<1 || username.length>20) {
                error_bar('用户名应在1-20位')
                return;
            }
            if(password.length<1 || password.length>20) {
                error_bar('密码应在1-20位')
                return;
            }
            $.post('/auth/api/login',{
                username: username,
                password: password,
            },function(data) {
                if(data.code != 0) {
                    error_bar(data.msg)
                    return
                }
                window.localStorage.setItem("uid",data.uid)
                window.localStorage.setItem("group",data.group)
                success_bar('登录成功！')
                setTimeout(() => {
                        window.location.href = '/user/set#first'
                }, 200);
                if (location.hash == undefined) location.hash=""
                if (location.hash != "") {
                    setTimeout(() => {window.location.href = location.hash.replace("#","")},200)
                    return}
                    if (password == "123456") {
                    setTimeout(() => {
                        window.location.href = '/user/set#first'
                    }, 200);// 给点时间把 cookie 存了，防止过去之后被重定向回来
                } else {
                    window.location.href = '/'
                }
            });
        }

        function register() {
            var username = document.getElementById('username').value
            var password = document.getElementById('password').value
            var repassword = document.getElementById('repassword').value
            if(username === '' || password === '') {
                error_bar('有东东没输入哦')
                return;
            }
            if(username.length<6 || username.length>20) {
                error_bar('用户名应在6-20位')
                return;
            }
            if(password.length<6 || password.length>20) {
                error_bar('密码应在6-20位')
                return;
            }
            if(password != repassword) {
                error_bar('两次密码输入不一致哦')
                return
            }
            $.post('/auth/api/reg',{
                username: username,
                password: password,
            },function(data) {
                if(data.code != 0) {
                    error_bar(data.msg)
                    return
                }
                success_bar('注册成功！')
                window.location.href = '/'
            });
        }
    </script>
</body>
</html>